/*
 * Copyright (c) 2003-2005
 * XDoclet Team
 * All rights reserved.
 */
package org.xdoclet.plugin.hibernate;

import com.thoughtworks.qdox.model.AbstractJavaEntity;
import com.thoughtworks.qdox.model.JavaField;
import com.thoughtworks.qdox.model.JavaMethod;
import com.thoughtworks.qdox.model.Type;

/**
 * bean representing hibernate property. it could be property getter, as well as
 * just a field (regardless private or public. hibernate does not care )
 */
public class HibernateProperty {
    private AbstractJavaEntity entity;
    private String access;
    private String name;

    public void setAccess(String access) {
        this.access = access;
    }

    /**
     * access specification. shall be field for fields
     */
    public String getAccess() {
        return access;
    }

    public void setEntity(AbstractJavaEntity entity) {
        this.entity = entity;
    }

    public AbstractJavaEntity getEntity() {
        return entity;
    }

    public void setName(String name) {
        this.name = name;
    }

    /**
     * name of hibernate property
     */
    public String getName() {
        return name;
    }

    /**
     * retrtieve corresponding type from property
     */
    public Type getType() {
        final AbstractJavaEntity en = getEntity();

        if (en instanceof JavaMethod) {
            return ((JavaMethod) en).getReturns();
        } else if (en instanceof JavaField) {
            return ((JavaField) en).getType();
        } else {
            return Type.VOID;
        }
    }

    /**
     * puprose of this equality method is to allow proprety overriding
     * properties are equal, if they have the same name, regardless of access.
     * Since we build property map from top to bottom of class hierarchy, we will reflect
     * java behaviour
     */
    public boolean equals(Object o) {
        if (o == null) {
            return false;
        }

        if (!(o instanceof HibernateProperty)) {
            return false;
        }

        return getName() != null && getName().equals(((HibernateProperty) o).getName());

        }

    public int hashCode() {
        if (getName() != null) {
            return getName().hashCode();
        }

        return super.hashCode();
    }

    public String toString() {
        return "[" + this.name + "][" + this.access + "]";
    }
}